import { Injectable } from '@nestjs/common';
import axios from 'axios';
import * as cheerio from 'cheerio';
import xlsx from 'node-xlsx';
import * as fs from 'fs';
@Injectable()
export class PriceService {
  async main() {
    const url = `http://quotes.money.163.com/f10/zycwzb_600519.html#01c01`;
    const data = await axios.get(url);
    const $ = cheerio.load(data.data);
    const titles = $(
      '.table_bg001.border_box.limit_sale.scr_table tbody tr:first-child',
    ).children();
    const profits = $(
      '.table_bg001.border_box.limit_sale.scr_table tbody tr:nth-child(12)',
    ).children();
    const titleArr = [],
      profitArr = [];
    for (let i = 0; i < titles.length; i++) {
      titleArr.push($(titles[i]).text());
      profitArr.push($(profits[i]).text());
    }
    const buffer = xlsx.build([{ name: '茅台', data: [titleArr, profitArr] }]);
    fs.writeFileSync('茅台.xlsx', buffer, 'buffer');
    return 'ok';
  }
}
